﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace BDRUtils.Cached
{
    public static class CachedExtensions
    {
        /// <summary>
        /// 注入Cached服务
        /// </summary>
        /// <param name="services"></param>
        /// <param name="cachedEnum">枚举类型</param>
        /// <param name="options">若为CachedEnum.Redis，则options不能为空</param>
        /// <returns></returns>
        public static IServiceCollection AddCacheService(this IServiceCollection services, CachedEnum cachedEnum, Action<RedisSettingOption> options)
        {
            switch (cachedEnum)
            {
                case CachedEnum.Memory:
                    services.TryAddSingleton<ICached, MemoryProvider>();
                    break;
                case CachedEnum.Redis:
                    if (options is null)
                        throw new ArgumentNullException(nameof(options));
                    services.Configure(options);
                    services.TryAddSingleton<RedisHelperBase>();
                    services.TryAddSingleton<ICached, RedisProvider>();
                    break;
            }

            return services;
        }
    }
}
